﻿// 创建Python引擎和作用域
using System;
using IronPython.Hosting;
using IronPython.Runtime;
using ironTest;
using Microsoft.Scripting.Hosting;

var engine = Python.CreateEngine();
var scope = engine.CreateScope();

try
{
    Console.WriteLine("IrronPython:python与C#互相调用");
    //engine.GetSysModule().SetVariable("path", engine.GetSearchPaths().Concat(new[] { "C:\\Users\\Jack\\source\\repos\\ironTest\\my_module.py" }));
    // 导入Python模块
    var source = engine.CreateScriptSourceFromString("import my_module");
    source.Execute(scope);

    // 获取Python模块中的变量和函数
    dynamic myModule = scope.GetVariable("my_module");
    dynamic myFunction = myModule.my_function;
    dynamic myA = myModule.A;
    dynamic myVariable = myModule.my_variable;

    // 调用Python函数
    myFunction();
    int a = myA(123);
    Console.WriteLine($"A:{a}");
    // 访问Python变量
    Console.WriteLine("Value of my_variable: " + myVariable);

    Console.WriteLine("========cmd调用执行========");
    cmdTest.Test();

    Console.ReadLine();
}
catch (Exception ex)
{
    Console.WriteLine("An error occurred: " + ex.Message);
}